var ContentLoader = function() {
}

ContentLoader.prototype.loadCategories = function(container) {
	var this_ = this;
	container = container || $('#cat');
	$.ajax({
		url : '/s/?action=load_content',
		data : 'q=get_cat',
		success : function(resp) {
			if (resp.success === true) {
				if (resp.cats) {
					for ( var i = 1; i < resp.cats.length; i++) {
						var catDiv = $('<div />');
						catDiv.addClass('cat-item');
						$(catDiv).click(function() {
							this_.viewCategory($(this).text());
						});
						$(catDiv).draggable();
						var catName = $('<span />', {
							text : resp.cats[i]
						});
						var randDeg = Math.floor((Math.random() * 20) - 10);
						$(catDiv).css('-webkit-transform', 'rotate(' + randDeg + 'deg)');
						$(catDiv).css('-moz-transform', 'rotate(' + randDeg + 'deg)');
						catName.addClass('cat-item-name');
						$(catDiv).html($(catName));
						$('#cat').append($(catDiv));
					}
				} else
					app.goHome();
			} else
				app.goHome();
		}
	});
}

ContentLoader.prototype.viewCategory = function(cat) {

}

ContentLoader.prototype.showPreloader = function() {
	$('#main-container').prepend('<img id="preloader" src="../static/images/preloader.gif" />')
}

ContentLoader.prototype.loadNext = function(current) {
	current = current || 10;
}

ContentLoader.prototype.loadInbox = function() {
	var m = window.messageHandler.messages;
	if (m.length != 0) {
		window.location.hash = '#inbox';
		this.showPreloader();
		$('#main-container').empty();
		for ( var i = 0; i < m.length; i++) {
			var mesDiv = $('<div />');
			mesDiv.addClass('message');
			var sender = $('<div />', {
				text : m[i].first
			});
			sender.addClass('sender');
			var receiver = $('<div />', {
				text : m[i].second
			});
			receiver.addClass('receiver');
			var contactMe = $('<div id="contact-me" class="button" onclick="(new MessageHandler()).openEditor(\''
					+ m[i].first + '\');"><span>Reply</span></div>');
			$(mesDiv).append($(sender), $(receiver), $(contactMe));
			$('#main-container').append($(mesDiv));
		}
	} else {
		$('#main-container').empty();
	}
	var getOldMsg = $('<div id="get-old" class="button" onclick="messageHandler.getOld();"><span>Old messages</span></div>');
	$('#main-container').append($(getOldMsg));

}

ContentLoader.prototype.loadOutbox = function() {
	var this_ = this;
		$.ajax({
				url : '/s/?action=outbox',
				success : function(resp) {
					if (resp.success === true) {
						var m = resp.outbox.msg || [];
						if (resp.outbox.length != 0) {
							window.location.hash = '#outbox';
							this_.showPreloader();
							$('#main-container').empty();
							for ( var i = 0; i < m.length; i++) {
								var mesDiv = $('<div />');
								mesDiv.addClass('message');
								var sender = $('<div />', {
									text : m[i].first
								});
								sender.addClass('sender');
								var receiver = $('<div />', {
									text : m[i].second
								});
								receiver.addClass('receiver');
								var contactMe = $('<div id="contact-me" class="button"	 onclick="(new MessageHandler()).openEditor(\''
										+ m[i].first + '\');"><span>Reply</span></div>');
								$(mesDiv).append($(sender), $(receiver), $(contactMe));
								$('#main-container').append($(mesDiv));
							}

						}
					}
				}
			});
}